/*
 * Sensors.c
 *
 *  Created on: Jun 8, 2010
 *      Author: Armen Nazarian
 */

#include <includes.h>
#include "../toolbox/toolbox.h"
#include "Sensors.h"
#include "../app_safetrucking_cfg.h"

void sensor_readings(void *p_arg) {
	(void) p_arg;
	INT8U first_sensor_wait = 0;
	INT8U second_sensor_wait = 0;
	INT8U three_sensor_wait = 0;
	INT8U forth_sensor_wait = 0;
	while (1) {

		INT8U readings = ~read_sensor();
		readings = readings & 0xF;

		//convertToBinaryFunctionAndDisplay(readings);

		if (readings == 1 && first_sensor_wait == 0) {
			//    	if(1) {

			OSSemPost(diode_one_start);
			diode1_toggle();

			first_sensor_wait = 1;
			second_sensor_wait = 0;
		} else if (readings >= 2 && readings <=3 && second_sensor_wait == 0) {

			OSSemPost(diode_one_stop);
			OSSemPost(diode_two_start);
			diode2_toggle();

			second_sensor_wait = 1;
			first_sensor_wait = 0;
		} else if (readings >= 4 && readings < 8 && three_sensor_wait == 0 ) {
			OSSemPost(diode_two_stop);

			OSSemPost(diode_three_start);
			diode3_toggle();
			three_sensor_wait = 1;
			second_sensor_wait = 0;

		} else if (readings >= 8 && forth_sensor_wait == 0) {
			OSSemPost(diode_three_stop);
			OSSemPost(diode_forth_start);
			diode4_toggle();


			OSSemPost(buzzer1_start);
			bulbs_toggle();
			buzzer1_turnon();


			forth_sensor_wait == 1;
			three_sensor_wait = 0;


		}
		OSTimeDly(OS_TICKS_PER_SEC / 10);
	}
}

